package vehicle;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class SyncVehicleTracker {

	private final ConcurrentMap<String,SynchronizedPoint> locations;
	private final Map<String,SynchronizedPoint> unmodifiableMap;
	
	public SyncVehicleTracker(Map<String,SynchronizedPoint> data) {
		locations = new ConcurrentHashMap<>(data);
		unmodifiableMap = Collections.unmodifiableMap(locations);
	}

	public void setLocation(String name, double x, double y) {
		if(locations.replace(name, new SynchronizedPoint(x,y)) == null)
			throw new IllegalArgumentException("invalid vehicle Id: " + name);
	}

	public SynchronizedPoint getLocation(String name) {
		return locations.get(name);
	}
	
	public Map<String,SynchronizedPoint> getLocations() {
		return unmodifiableMap;
	}
	
	public Map<String,SynchronizedPoint> getLocaltionAsStatic() {
		return Collections.unmodifiableMap(new HashMap<String,SynchronizedPoint>(locations));
	}
	
}
